//
//  CZMainTabbar.swift
//  XLWB009
//
//  Created by apple on 3/20/16.
//  Copyright © 2016 apple. All rights reserved.
//

import UIKit

let count=5
class CZMainTabbar: UITabBar {
    
    //按钮点击的闭包 可变的闭包
    var composeClosure:(() -> ())?

    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算itme的宽度
        let width=frame.width/CGFloat(count)
        
        //计算按钮之间的间距
        let orgin=CGRect(x: 0, y: 0, width: width, height:frame.height)
        
        var index=0
        for view in subviews{
            
            //判断是否UITabBarButton
            let cls=NSClassFromString("UITabBarButton")
            if view.isKindOfClass(cls!)
            {
            
                view.frame=CGRectOffset(orgin, CGFloat(index)*width, 0)
                
                index += index == 1 ? 2 :1
            }
        }
        //设置加号按钮frame
        comPoseButton.frame=CGRect(x: 2*width, y: 0, width: width, height: frame.height)
    }
    
    //懒加载
    //添加按钮
    private lazy var comPoseButton:UIButton={
        let button=UIButton()
        
        //设置
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
     
        //添加到button上面
        self.addSubview(button)
        
        //为按钮添加点击事件
        button.addTarget(self, action: "composeClik", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()

    //调用按钮点击事件闭包
    func composeClik(){
        
        //uiview是不能直接弹出控制器的
        //调出闭包，？如果闭包没有值就不执行
        composeClosure?()
    }
}
